<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateGovernmentDevicesTable extends Migration
{
    /**
     * Run the migrations.
     *运营管理->基础数据导入->环卫部件导入->市政部件导入
     *
     * @return void
     */
    public function up()
    {
        Schema::create('government_device_categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->comment('设备类型名称')->nullable();
            $table->char('property',20)->comment('类型属性 0点 1面')->default(1);
            $table->string('color',20)->comment('颜色')->nullable();
            $table->integer('sort')->unsigned()->comment('排序')->default(100);
            $table->softDeletes();
            $table->timestamps();
        });

        Schema::create('government_devices', function (Blueprint $table) {
            $table->increments('id');
            // $table->string('name')->comment('市政名称')->nullable();
            $table->string('name')->comment('设备名称')->nullable();
            $table->string('mark',20)->comment('编号')->nullable();
            $table->string('address')->comment('地址')->nullable();
            $table->integer('type_id')->unsigned()->comment('设备类型id')->nullable();
            $table->foreign('type_id')->references('id')->on('government_device_categories')->onDelete('set null');
            $table->string('device_tag')->comment('设备标号')->nullable();
            // $table->float('lon',8,5)->comment('经度,保留 5 位小数')->nullable();
            // $table->float('lat',8,5)->comment('纬度,保留 5 位小数')->nullable();
            $table->point('location')->comment('经纬度-点')->nullable();
            $table->polygon('areas')->comment('护栏-面')->nullable();
            $table->string('specs')->comment('规格')->nullable();
            $table->integer('number')->unsigned()->comment('数量')->nullable();
            $table->char('unit',20)->comment('单位')->nullable();
            $table->string('factory',20)->comment('厂家')->nullable();
            $table->string('contacts',20)->comment('联系人')->nullable();
            $table->string('mobile',20)->comment('手机号码')->nullable();
            $table->string('property_company',20)->comment('产权单位')->nullable();
            // $table->timestamp('service_time')->comment('维护时间')->nullable(true);
            // $table->timestamp('last_time')->comment('上次维护时间')->nullable(true);
            // $table->timestamp('next_time')->comment('下次维护时间')->nullable(true);
            // $table->integer('department_id')->comment('负责部门id')->unsigned()->nullable();
            // $table->foreign('department_id')->references('id')->on('departments')->onDelete('set null');
            $table->integer('principal_id')->unsigned()->comment('负责人id')->nullable();
            $table->foreign('principal_id')->references('id')->on('employees')->onDelete('set null');
            $table->string('phone',20)->comment('电话')->nullable();
            $table->integer('rfid')->unsigned()->comment('RFID绑定')->nullable();
            $table->softDeletes();
            $table->timestamps();
        });

        //市政部件附件管理
        Schema::create('government_device_files', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('government_id')->unsigned()->comment('市政部件id')->nullable();
            $table->foreign('government_id')->on('government_devices')->references('id')->onDelete('set null');
            $table->BigInteger('file_id')->unsigned()->comment('图片所属的文件信息')->nullable();
            $table->foreign('file_id')->on('file_upload')->references('id')->onDelete('set null');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('government_device_files');
        Schema::dropIfExists('government_devices');
        Schema::dropIfExists('government_device_categories');
    }
}
